﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MyInput : MonoBehaviour {

    int clicklayer = 0;
	// Use this for initialization
	void Start ()
    {
        clicklayer = 1 << LayerMask.NameToLayer("MouseClick");
    }
    public static bool Enable = true;
	
	// Update is called once per frame
	void Update ()
    {
        if (!Enable) 
        {
            return;
        }
        if(LeftController.instance.IsInMoving)
        {
            return;
        }
        if (Input.touchCount > 0 )
        {
            for (int i = 0; i < Input.touchCount;i++)
            {
                if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
                {
                    return;
                }
            }
        }
        if(UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
        if(GridInfo.Instance == null)
        {
            return;
        }
//        if(Input.GetMouseButtonUp(0))
//        {
//            Vector2 v = Input.mousePosition;
//            Vector3 wp = Camera.main.ScreenToWorldPoint(v);
//            Collider2D c2d = Physics2D.OverlapPoint(wp, clicklayer);
//            if(c2d != null)
//            {
//                var thegridBehaviour = c2d.transform.parent.GetComponent<MatterGridBehaviour>();
                //                MainGame.Instance.TestEnergeChange(1000000.0f);
                //                thegridBehaviour.mg.EnergyChange(1000000.0f);
//                return;

//            }
//        }
        if (Input.GetMouseButton(0))
        {
            Vector2 v = Input.mousePosition;
            Vector3 wp = Camera.main.ScreenToWorldPoint(v);
            Collider2D c2d = Physics2D.OverlapPoint(wp, clicklayer);
            if (c2d != null)
            {
                var thegridBehaviour = c2d.transform.parent.GetComponent<MatterGridBehaviour>();
                GridInfo.Instance.SetMatterGrid(thegridBehaviour.mg, thegridBehaviour.transform.position);
                //                MainGame.Instance.Scene.AddShiTou(thegridBehaviour.mg.X, thegridBehaviour.mg.Y);
//                Debug.Log("AddShitou");
//                return;
            }
        }
        if (Input.GetMouseButtonUp(1))
        {
//            Vector2 v = Input.mousePosition;
//            Vector3 wp = Camera.main.ScreenToWorldPoint(v);
//            Collider2D c2d = Physics2D.OverlapPoint(wp, clicklayer);
//            if (c2d != null)
//            {
//                var thegridBehaviour = c2d.transform.parent.GetComponent<MatterGridBehaviour>();
//                MainGame.Instance.Scene.RemoveSolid(thegridBehaviour.mg.X, thegridBehaviour.mg.Y);
//                Debug.Log("RemoveSolid");
//                return;
//            }
        }
    }
}
